package modules
{
import core.StageProxy;
import core.model.GameModel;
import core.model.SystemModel;
import core.model.events.SystemModelEvent;
import core.resources.PlayerDataResourceTask;

import flash.events.Event;

import framework.core.ComponentBuilder;
import framework.dao.EntryUtil;

import modules.createplayer.CreatePlayerUI;
import modules.createplayer.value.CreatePlayerSettingValue;


public class CreatePlayerModule extends ModuleBase
{
	public function CreatePlayerModule()
	{
		super();
	}
    //==========================================================================
    //  Variables
    //==========================================================================
    private var ui:CreatePlayerUI;
    
    private function get systemModel():SystemModel
    {
        return GameModel.instance.systemModel;
    }
    //==========================================================================
    //  Overridden methods: ModuleBase
    //==========================================================================
	override protected function initConfigComplete():void
	{
        super.initConfigComplete();
        
		var settingValue:CreatePlayerSettingValue = EntryUtil.mapping(XML(configList[data.datas[0].url]) , CreatePlayerSettingValue);
		
		ui = ComponentBuilder.create(CreatePlayerUI) as CreatePlayerUI;
        ui.data = settingValue;
		addChild(ui);
        
        systemModel.addEventListener(SystemModelEvent.INITED, initedHandler);
        
        inited();
	}
    
    override public function dispose():void
    {
        systemModel.removeEventListener(SystemModelEvent.INITED, initedHandler);
        
        super.dispose();
    }
    
    private function initedHandler(event:Event):void
    {
        var loader:PlayerDataResourceTask = new PlayerDataResourceTask();
        loader.addEventListener(Event.COMPLETE, playerDataReadyHandler);
        loader.start();
    }
    
    private function playerDataReadyHandler(event:Event):void
    {
        var loader:PlayerDataResourceTask = event.target as PlayerDataResourceTask;
        loader.removeEventListener(Event.COMPLETE, playerDataReadyHandler);
        
        if (this.parent)
        {
            this.parent.removeChild(this);
        }
        dispose();
        
        StageProxy.unlock();
        
        Game.instance.start();
    }
}
}





